<?php

include_once "Model.php";

class Record extends Model
{

    public static $_instance;
    private $data = array();

    public static function model()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function __set($name, $value)
    {
        $this->data[$name] = $value;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
                'Undefined property via __get(): ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'], E_USER_NOTICE);
        return null;
    }

    function save()
    {
        return $query = $this->insert("
			INSERT INTO gadata (landing_page, page_title, page_views, visits, visitors, bounce_rate, pageviews_per_visit, time_on_page, avg_time_on_page, day, month, year, hour)
			VALUES (:landing_page, :page_title, :page_views, :visits, :visitors, :bounce_rate, :pageviews_per_visit, :time_on_page, :avg_time_on_page, :day, :month, :year, :hour)", $this->data);
    }

    function getRecords($day, $month, $year)
    {
        $sql = "SELECT id,landing_page, page_title, page_views FROM gadata WHERE day=:day AND month=:month AND year=:year ORDER BY page_views desc";
        $params['day'] = $day;
        $params['month'] = $month;
        $params['year'] = $year;

        return $this->queryOne($sql, $params);
    }

    function buildRankingQuery($id, $rank)
    {
        $sql = "UPDATE gadata SET rank=$rank WHERE id=$id;";
        return $sql;
    }

    function updateRank(array $update_sql)
    {
        return $this->exeUpdateRank($update_sql);
    }

}

